package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @auther :XJY
 * @date :2022/8/14$ 19:34$
 * @description :  web层 用于接收前端发出的请求
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /***
     *功能描述 : 登录功能
     *@return : com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        //employeeService.getObj()
        //QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        //queryWrapper.eq("username","admin");
        //Employee emp = employeeService.getOne(queryWrapper);
        Employee emp = employeeService.getOne(queryWrapper);

        log.info(String.valueOf(emp));
        //System.out.println(emp);
        //3. 如果没有查询到, 则返回登录失败结果
        if (emp == null) {
            return R.error("登录失败");
        }
        //4. 密码比对，如果不一致, 则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        //5. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //6. 登录成功，将员工id存入Session,
        HttpSession session = request.getSession();
        session.setAttribute("employee", emp.getId());
        //7. 返回登录成功结果
        return R.success(emp);
    }

    /***
     *功能描述 : 退出功能
     *@return : com.itheima.reggie.common.R<java.lang.String>
     */

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return R.success("退出成功");
    }

    /***
     *功能描述 : 添加员工
     *@return : com.itheima.reggie.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工");

        //1.接收数据 将密码进行MD5算法

        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        //2.修改数据
        //2.1给对象中添加创建人 修改人
        //Long adminId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(adminId);
        //employee.setUpdateUser(adminId);
        //2.2 向对象中添加创建时间 修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //3.保存到数据库
        employeeService.save(employee);
        return R.success("添加成功");


    }

    /***
     *功能描述 : 分页查询
     *@return : com.itheima.reggie.common.R<java.lang.String>
     */
    @GetMapping("/page")
    public R<Page> selectByCondition(Integer page, Integer pageSize, Integer name) {

        //1.添加分页信息
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //2.构造分页查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        // 姓名不空 添加到条件中
        lqw.like(!StringUtils.isEmpty(name), Employee::getName, name);
        //3.构造排序条件
        //根据创建时间降序查询
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage, lqw);

        return R.success(employeePage);
    }

    /***
     *功能描述 :  修改员工信息 修改状态 跟修改信息 都用此方法实现
     * @param request
     * @param employee
     *@return : com.itheima.reggie.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> upData(HttpServletRequest request, @RequestBody Employee employee) {

        //Long empId = (Long) request.getSession().getAttribute("employee");
        //修改数据
        //添加修改人信息
       // employee.setUpdateUser(empId);
        //获取前端传送的数据 根据Id 修改数据
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /***
     *功能描述 : 根据id查询用户信息 用于修改数据时的回显
     * @param id
     *@return : com.itheima.reggie.common.R<com.itheima.reggie.entity.Employee>
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Long id) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getId, id);
        Employee employee = employeeService.getOne(lqw);
        return R.success(employee);
    }





}
